package com.booby.bootweb.controller;

import com.booby.bootweb.model.ExtDict;
import com.booby.bootweb.model.ExtStopword;
import com.booby.bootweb.service.EsKeywordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 功能描述:
 * https://www.cnblogs.com/archermeng/p/7537037.html
 * https://codingnote.cc/p/201888
 * https://www.codenong.com/jsed925584592a/
 * @author: lizt
 * @date: 2020/9/14 14:24
 **/

@RestController
@RequestMapping("/ext")
public class EsKeywordController {

    @Resource
    private EsKeywordService esKeywordService;

    @RequestMapping(value = "/dic.txt", method = RequestMethod.GET)
    public void loadDic(HttpServletRequest req, HttpServletResponse response){
        OutputStream out=null;
        try {
            out = response.getOutputStream();
            List<ExtDict> list = esKeywordService.getDicAll();
            StringBuilder sb = new StringBuilder();
            for (ExtDict dict: list) {
                if (StringUtils.isNotBlank(dict.getKeyword())){
                    sb.append(dict.getKeyword() + "\n");
                }

            }
            String data = sb.toString();
            // ES 要求的 Last-Modified 和 ETag 返回Header信息
            response.setHeader("Last-Modified", String.valueOf(System.currentTimeMillis()));
            response.setHeader("ETag",String.valueOf(System.currentTimeMillis()));
            response.setContentType("text/plain; charset=utf-8");
            // 输出content-length 头信息  否则默认输出 "Transfer-Encoding: chunked" 导致无法更新词库
            response.setContentLength(data.getBytes("utf-8").length);
            out.write(data.getBytes("utf-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @RequestMapping(value = "/stopkeyword.txt", method = RequestMethod.GET)
    public void loadStopword(HttpServletRequest req, HttpServletResponse response){
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            List<ExtStopword> list = esKeywordService.getStopwordAll();
            StringBuilder sb = new StringBuilder();
            for (ExtStopword dict: list) {
                if (StringUtils.isNotBlank(dict.getKeyword())){
                    sb.append(dict.getKeyword() + "\n");
                }
            }
            String data = sb.toString();
            // ES 要求的 Last-Modified 和 ETag 返回Header信息
            response.setHeader("Last-Modified", String.valueOf(System.currentTimeMillis()));
            response.setHeader("ETag",String.valueOf(System.currentTimeMillis()));
            response.setContentType("text/plain; charset=utf-8");
            // 输出content-length 头信息  否则默认输出 "Transfer-Encoding: chunked" 导致无法更新词库
            response.setContentLength(data.getBytes("utf-8").length);
            out.write(data.getBytes("utf-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
